package secret.santa.server;

import java.util.ArrayList;
import java.util.Collection;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import secret.santa.client.CS_Event;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DB_Event
{	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	private String encodedKey;
	
	@Persistent
	private String name;
	
	@Persistent
	private String description;

	@Persistent
	private ArrayList<String> participants;
	
	public CS_Event toClient()
	{
		CS_Event c = new CS_Event();
		
		c.encodedKey = encodedKey;
		c.name = name;
		c.description = description;
		if(participants != null)
			c.participants = participants.toArray( new String[participants.size()]);
		else
			c.participants = null;
		return c;
	}
	
	/**
	 * @param name the name to set
	 */
	public void setName(String name) 
	{
		this.name = name;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param encodedKey the encodedKey to set
	 */
	public void setEncodedKey(String encodedKey) {
		this.encodedKey = encodedKey;
	}

	/**
	 * @return the encodedKey
	 */
	public String getEncodedKey() {
		return encodedKey;
	}

	/**
	 * @param participants the participants to set
	 * @gwt.typeArgs participants <java.lang.String>
	 */
	public void setParticipants(ArrayList<String> participants) {
		this.participants = participants;
	}

	/**
	 * @return the participants
	 * @gwt.typeArgs <java.lang.String>
	 */
	public ArrayList<String> getParticipants() 
	{
		if(participants == null)
		{
			participants = new ArrayList<String>();
		}
		return participants;
	}
	public void addParticipants(Collection<String> c)
	{
		//ensure uniqueness
		//TODO: oh my gawd performance
		participants.removeAll(c);
		participants.addAll(c);
	}

}
